Ext.define('BM.controller.BrowserButtons', {
    extend: 'Ext.app.Controller',
	//views: ['main.FilterButton'],
	stores:['Tokens','Boxs'],
	tokenFilterId:'bath-filter-all',
	boxFilterId:'bath-filter-all',
	tabPanelId:'bath-tab-token',
	tokenGroupId:'token-group-male',
	boxGroupId:'box-group-luxury',
	objFiltBtn:null,
    init: function() {
    
         this.control({
            'tabpanel':{
            	tabchange:this.onTabChange
            },
            'tokengroupbutton>button':{
            	click:this.onTokenGroupButtonClick
            },
            'boxgroupbutton>button':{
            	click:this.onBoxGroupButtonClick
            },
            'filterbutton>button':{
            	click:this.onFilterButtonClick
            },
             'tokengroupbutton>button':{
            	click:this.onTokenGroupButtonClick
            },
            'tokengroupbutton>textfield':{
            	specialkey:this.onTokenEnterKey
            	//change:this.onTokeSearch,
            	//buffer: 5000,
            	//delay:5000
            },
            'boxgroupbutton>button':{
            	click:this.onBoxGroupButtonClick
            },
            'boxgroupbutton>textfield':{
            	specialkey:this.onBoxEnterKey
            }
        });
    },
    //更换Tab标签时，触发事件,同时对应过滤的按钮
    onTabChange:function(tab,_new,_old){
    
    	this.tabPanelId = _new.id;
    	if(_new.id=='bath-tab-token')
    	this.onChangeFilterBtn(this.tokenFilterId);
    	else if(_new.id=='bath-tab-box')
    	this.onChangeFilterBtn(this.boxFilterId);
    },
    //按过滤按钮时
    onFilterButtonClick:function(_btn,e){
    
    	if(this.objFiltBtn==null)
    	this.objFiltBtn = _btn.up('filterbutton');
    	
    	var _btnId = _btn.id;
    	this.onChangeFilterBtn(_btnId);
    	
    	if(_btnId=='bath-filter-all'||_btnId=='bath-filter-refresh'){
    	
    		if(this.tabPanelId=='bath-tab-token'){
	    		this.onTokenStoreLoad('');
	    		this.tokenFilterId = _btnId;
    		}else if(this.tabPanelId=='bath-tab-box'){
	    		this.onBoxStoreLoad('');
	    		this.boxFilterId = _btnId;
    		}
    	}else if(_btnId=='bath-filter-free'){
    	
    		if(this.tabPanelId=='bath-tab-token'){
	    		this.tokenFilterId = _btnId;
	    		this.onTokenStoreLoad('1');
    		}
    		else if(this.tabPanelId=='bath-tab-box'){
	    		this.boxFilterId = _btnId;
	    		this.onBoxStoreLoad('0');
    		}
    	}else if(_btnId=='bath-filter-run'){
    	
    		if(this.tabPanelId=='bath-tab-token'){
	    		this.tokenFilterId = _btnId;
	    		this.onTokenStoreLoad('2');
    		}
    		else if(this.tabPanelId=='bath-tab-box')
    		{
	    		this.boxFilterId = _btnId;
	    		this.onBoxStoreLoad('1');
    		}
    	}else if(_btnId=='bath-filter-book'){
    	
    		if(this.tabPanelId=='bath-tab-token'){
	    		this.tokenFilterId = _btnId;
	    		this.onTokenStoreLoad('3');
    		}
    		else if(this.tabPanelId=='bath-tab-box'){
	    		this.boxFilterId = _btnId;
	    		this.onBoxStoreLoad('2');
    		}
    	}else if(_btnId=='bath-filter-stop'){
    	
    		if(this.tabPanelId=='bath-tab-token'){
	    		this.tokenFilterId = _btnId;
	    		this.onTokenStoreLoad('4');
    		}
    		else if(this.tabPanelId=='bath-tab-box'){
	    		this.boxFilterId = _btnId;
	    		this.onBoxStoreLoad('3');
    		}
    	}
    },
    onTokenStoreLoad:function(_value){
    
    	this.getTokensStore().clearFilter();
    	
    	//if(_value!="")
    	//this.getTokensStore().filter('handStatus_FK', _value);
    	if(this.tokenGroupId == 'token-group-male'){
    	
    		this.getTokensStor().load({params:{typeFlag:'1',handStatus:_value}});
    	}else if(this.tokenGroupId == 'token-group-male'){
    		this.getTokensStor().load({params:{typeFlag:'0',handStatus:_value}});
    	}
    },
    onBoxStoreLoad:function(_value){
    
    	this.getBoxsStore().clearFilter();
    	if(_value!="")
    	this.getBoxsStore().filter('handStatus_FK', _value);
    },
    onChangeFilterBtn:function(_btnId){
    	
    	if(this.objFiltBtn!=null){
	    	this.objFiltBtn.down("#bath-filter-all").enable();
			this.objFiltBtn.down("#bath-filter-free").enable();
			this.objFiltBtn.down("#bath-filter-run").enable();
			this.objFiltBtn.down("#bath-filter-book").enable();
			this.objFiltBtn.down("#bath-filter-stop").enable();
			this.objFiltBtn.down("#bath-filter-refresh").enable();
	    	this.objFiltBtn.down("#"+_btnId).disable();
    	}
    },
    //按手牌上方分类按钮时
    onTokenGroupButtonClick:function(_btn,e){
    
    	
    	var _btnId = _btn.id;
    	if(_btnId=='token-group-male'||
    		_btnId=='token-group-female'||
    			_btnId=='token-group-search'){
    			alert("token");
    		this.tokenGroupId = _btnId;
    		this.tokenFilterId = "bath-filter-all";
    		this.onChangeFilterBtn("bath-filter-all");
    	}
    },
    //按包间上方分类按钮时
     onBoxGroupButtonClick:function(_btn,e){
     
     	var _btnId = _btn.id;
     	if(_btnId=='box-group-luxury'||
     		_btnId=='box-group-common'||
     		_btnId=='box-group-search'
     	){
     		
     		this.boxGroupId = _btnId;
     		this.boxFilterId = "bath-filter-all";
     		this.onChangeFilterBtn("bath-filter-all");
     	}
     },
    
    //按手牌上方分类按钮时
    onTokenGroupButtonClick:function(_btn,e){
    
    	
    	var _btnId = _btn.id;
    
    	if(_btnId=='token-group-male'){
    	
    		 _btn.up('tokengroupbutton').down("#token-group-female").enable();
    		 _btn.disable();
    		this.getTokensStore().clearFilter();
    		
    		this.getTokensStore().load({params:{typeFlag:1,handStatus:1}});
    		//this.getTokensStore().filter('typeFlag', '1');
    		
    		this.tokenGroupId = _btnId;
    		this.onChangeFilterBtn("bath-filter-all");
    	}else if(_btnId=='token-group-female'){
    	
    		 _btn.up('tokengroupbutton').down("#token-group-male").enable();
    		_btn.disable();
    		
    		this.getTokensStore().clearFilter();
    		this.getTokensStore().load({params:{typeFlag:0,handStatus:1}});
    		//this.getTokensStore().filter('typeFlag', '2');
    		
    		this.tokenGroupId = _btnId;
    		this.onChangeFilterBtn("bath-filter-all");
    	}else if(_btnId=='token-group-search'){
			
			if( _btn.up('tokengroupbutton').down('textfield').value == "undefined"||
			    _btn.up('tokengroupbutton').down('textfield').value == "")
			{
			   Ext.Msg.alert("系统提示","请您输入要查的手");
			}else{
			
				_btn.up('tokengroupbutton').down("#token-group-male").enable();
				_btn.up('tokengroupbutton').down("#token-group-female").enable();
				
	    		this.getTokensStore().sort('handTokenNum', 'DESC');
	    		var _node = Ext.widget('tokenbrowser').getNodes(0,2);
	    		this.tokenGroupId = _btnId;
    			this.onChangeFilterBtn("bath-filter-all");
			}
    	}
    },
    //按回车时查询Token
    onTokenEnterKey:function(f,e){
    
    	if(e.getKey() == e.ENTER){
    	
    		var _newValue = f.getValue();
    		if(_newValue=="")
    		{
   				Ext.Msg.alert("系统提示","请输入要查的手牌编号！");
   			    return;
    		}
    		//GROUP全部打开，Filter为全部
    		
    	
    		var _store = this.getTokensStore();
    		
	        _store.suspendEvents();
	        _store.clearFilter();
	        
	        Ext.widget('tokenbrowser').getSelectionModel().clearSelections();
	        
	        _store.resumeEvents();
	        _store.filter({
	            property: 'handTokenNum',
	            anyMatch: true,
	            value   : _newValue
	        });
    	
    	}
    },
    onTokeSearch:function(field, newValue){
    
    	Ext.Msg.alert("test",newValue);
    },
    //按包间上方分类按钮时
     onBoxGroupButtonClick:function(_btn,e){
     	
    	var _btnId = _btn.id;
    	
    	if(_btnId=='box-group-luxury'){
    		 _btn.up('boxgroupbutton').down("#box-group-common").enable();
    		 _btn.disable();
    		 this.boxGroupId = _btnId;
    		 this.onChangeFilterBtn("bath-filter-all");
    	}else if(_btnId=='box-group-common'){
    		_btn.up('boxgroupbutton').down("#box-group-luxury").enable();
    		_btn.disable();
    		
    		this.boxGroupId = _btnId;
    		this.onChangeFilterBtn("bath-filter-all");
    	}else if(_btnId=='box-group-search'){
    	    _btn.up('boxgroupbutton').down("#box-group-luxury").enable();
    	    _btn.up('boxgroupbutton').down("#box-group-common").enable();
    	    
    	    this.boxGroupId = _btnId;
    		this.onChangeFilterBtn("bath-filter-all");
    	}
     },
    //按回车时查询Box
    onBoxEnterKey:function(f,e){
    
    	if(e.getKey() == e.ENTER){
    	
    		var _newValue = f.getValue();
    		if(_newValue=="")
    		{
   				Ext.Msg.alert("系统提示","请输入要查的包间编号！");
   			    return;
    		}
    		//GROUP全部打开，Filter为全部
    		
    	}
    }
});